/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Rewriting;

import SPARQL.TriplePattern;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Mirko
 */
public class SetUtils {
    
    public static <T> Set<Set<T>> powerSet(Set<T> originalSet) {
    Set<Set<T>> sets = new HashSet<Set<T>>();
    if (originalSet.isEmpty()) {
    	sets.add(new HashSet<T>());
    	return sets;
    }
    List<T> list = new ArrayList<T>(originalSet);
    T head = list.get(0);
    Set<T> rest = new HashSet<T>(list.subList(1, list.size())); 
    for (Set<T> set : powerSet(rest)) {
    	Set<T> newSet = new HashSet<T>();
    	newSet.add(head);
    	newSet.addAll(set);
    	sets.add(newSet);
    	sets.add(set);
    }		
    return sets;
}

public static <T> boolean setEquals(Set<T> set1 , Set<T> set2){
    if (set1.size()==0 && set2.size()==0)
        return true;
    if (set1.size()!= set2.size())
        return false;
    Set<T> set1clone = new HashSet<>();
    Set<T> set2clone = new HashSet<>();
    for (T element1 : set1)
        set1clone.add(element1);
    for (T element2 : set2)
        set2clone.add(element2);
    
    boolean test = true;
    for (T element1 : set1clone)
             test&=set2clone.remove(element1);
            
    
    
    
    return test;
    
}

public static <T> boolean setEqualsfuffa(Set<T> set1 , Set<T> set2){
    if (set1.size()==0 && set2.size()==0)
        return true;
    if (set1.size()!= set2.size())
        return false;
    
    
    boolean test = true;
    for (T element1 : set1)
              for (T element2 : set2){
            test&=set2.contains(element1);
            System.out.println("contain elements" + set2.remove(element1) + element1 + set2);
                  System.out.println("elements "+ element1 + element2);
                  System.out.println("equals " + element1.equals(element2));
                  System.out.print("hash equals ");
                  System.out.println(element1.hashCode()==element2.hashCode());
    
    }
    
        return test;
    
}
}
